package com.smartens.boit.activity;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.smartens.boit.R;
import com.smartens.boit.base.BaseActivity;
import com.smartens.boit.bean.GetDeviceGateWayDetailResult;
import com.smartens.boit.util.HttpHelper;

import java.text.BreakIterator;
import java.util.List;

/**
 * @Description 设备信息
 * @Author songtt
 * @Date 2021/3/4
 */
public class EquipmentInfoActivity extends BaseActivity {
    private LinearLayout name;
    private LinearLayout selectSingle;
    private LinearLayout selectMultiple;
    private ImageView back;
    private TextView equipmentType;
    private String typeText;
    private EditText aVoltage;
    private EditText bVoltage;
    private EditText cVoltage;
    private EditText aFlow;
    private EditText bFlow;
    private EditText cFlow;
    private TextView enabling;
    private String enableText;
    private TextView name_text;
    private TextView mactext;
    private TextView deviceVendorsText;
    private TextView factoryOpentypeText;
    private TextView unitopentypeText;
    private TextView ACspecificationsText;
    private TextView initialVoltageText;
    private TextView initialElectricText;

    //obj: "{\"getwayMac\":\"37FFD4054155353518890757\"}"
//DeviceMac mac
//DeviceName 设备名称
//DevicePhaseType 设备类型 ？
//DeviceVendors 制造商
//设备使能？
//DeviceRatedVoltage1 a相电压
//DeviceRatedCurrent a相电流
//factoryOpentype 出厂空开型号
//unitOpentype 使用单位空开型号
//ACspecifications 交流器规格
//initialElectric: "19" 初始电流
//initialVoltage: "194" 初始电压
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_equipment_info);

        initView1();


    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onResume() {

        super.onResume();
        //   Log.v("====","onResume");
        String mac = getIntent().getStringExtra("mac");
        initData1();
        new Thread() {
            @Override
            public void run() {
                super.run();
                initData2(mac);
            }
        }.start();

    }

    public void initView1() {

        ACspecificationsText=findViewById(R.id.ACspecificationsText);
        initialElectricText=findViewById(R.id.initialElectricText);
        initialVoltageText=findViewById(R.id.initialVoltageText);
        unitopentypeText=findViewById(R.id.unitOpentype);
        factoryOpentypeText=findViewById(R.id.factoryOpentype);
        name_text = findViewById(R.id.name_text);
        name = findViewById(R.id.change_name);
        mactext=findViewById(R.id.mactext);
        deviceVendorsText=findViewById(R.id.DeviceVendors);
        selectSingle = findViewById(R.id.select_single);
        selectMultiple = findViewById(R.id.multiple_select);
        equipmentType = findViewById(R.id.equipment_type);
        back = findViewById(R.id.tv_back);
        aFlow = findViewById(R.id.a_flow);
        bFlow = findViewById(R.id.b_flow);
        cFlow = findViewById(R.id.c_flow);
        aVoltage = findViewById(R.id.a_voltage);
        bVoltage = findViewById(R.id.b_voltage);
        cVoltage = findViewById(R.id.c_voltage);
        enabling = findViewById(R.id.tv_enabling);

        name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(EquipmentInfoActivity.this, ChangeNameActivity.class);

                startActivity(intent);
            }
        });
        selectSingle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(EquipmentInfoActivity.this, SingleTypeActivity.class);
                intent.putExtra("type", equipmentType.getText().toString());
                startActivityForResult(intent, 1000);
            }
        });
        selectMultiple.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(EquipmentInfoActivity.this, MultipleActivity.class);
                intent.putExtra("type", equipmentType.getText().toString());
                intent.putExtra("enabling", enabling.getText().toString());
                startActivityForResult(intent, 1000);
            }
        });
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EquipmentInfoActivity.this.finish();
            }
        });
    }

    @SuppressLint("SetTextI18n")
    public void initData1() {
        typeText = equipmentType.getText().toString();
        if ("单相单控".equals(typeText)) {
            aFlow.setText("10");
            aVoltage.setText("240");
            bFlow.setText("--");
            cFlow.setText("--");
            bVoltage.setText("--");
            cVoltage.setText("--");
            bFlow.setInputType(InputType.TYPE_NULL);
            cFlow.setInputType(InputType.TYPE_NULL);
            bVoltage.setInputType(InputType.TYPE_NULL);
            cVoltage.setInputType(InputType.TYPE_NULL);
            enabling.setText("A相");
        } else if ("单相三控".equals(typeText)) {
            aFlow.setText("10");
            aVoltage.setText("240");
            bFlow.setText("--");
            cFlow.setText("--");
            bVoltage.setText("--");
            cVoltage.setText("--");
            bFlow.setInputType(InputType.TYPE_NULL);
            cFlow.setInputType(InputType.TYPE_NULL);
            bVoltage.setInputType(InputType.TYPE_NULL);
            cVoltage.setInputType(InputType.TYPE_NULL);
            enabling.setText("A相");
        } else if ("三相单控".equals(typeText)) {
            aFlow.setText("10");
            aVoltage.setText("240");
            bFlow.setText("0");
            cFlow.setText("0");
            bVoltage.setText("0");
            cVoltage.setText("0");
            bFlow.setInputType(InputType.TYPE_CLASS_TEXT);
            cFlow.setInputType(InputType.TYPE_CLASS_TEXT);
            bVoltage.setInputType(InputType.TYPE_CLASS_TEXT);
            cVoltage.setInputType(InputType.TYPE_CLASS_TEXT);

        } else if ("三相三控".equals(typeText)) {
            aFlow.setText("10");
            aVoltage.setText("240");
            bFlow.setText("0");
            cFlow.setText("0");
            bVoltage.setText("0");
            cVoltage.setText("0");
            bFlow.setInputType(InputType.TYPE_CLASS_TEXT);
            cFlow.setInputType(InputType.TYPE_CLASS_TEXT);
            bVoltage.setInputType(InputType.TYPE_CLASS_TEXT);
            cVoltage.setInputType(InputType.TYPE_CLASS_TEXT);
        }
    }

    public void initData2(String mac) {
        HttpHelper httpHelper = HttpHelper.getInstance();
        httpHelper.GetDeviceGateWayDetail(mac, new HttpHelper.HttpCallBack<GetDeviceGateWayDetailResult>() {
            @Override
            public void onSuccess(List<GetDeviceGateWayDetailResult> result) {
                GetDeviceGateWayDetailResult getDeviceGateWayDetailResult = result.get(0);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        aFlow.setText(getDeviceGateWayDetailResult.getDeviceRatedCurrent()+"");
                        aVoltage.setText(getDeviceGateWayDetailResult.getDeviceRatedVoltage1()+"");
                        name_text.setText(getDeviceGateWayDetailResult.getDeviceName());
                        mactext.setText(getDeviceGateWayDetailResult.getDeviceMac());
                        deviceVendorsText.setText(getDeviceGateWayDetailResult.getDeviceVendors());
                        factoryOpentypeText.setText(getDeviceGateWayDetailResult.getFactoryOpentype()+"");
                        unitopentypeText.setText(getDeviceGateWayDetailResult.getUnitOpentype()+"");
                        ACspecificationsText.setText(getDeviceGateWayDetailResult.getACspecifications());
                        initialElectricText.setText(getDeviceGateWayDetailResult.getInitialElectric());
                        initialVoltageText.setText(getDeviceGateWayDetailResult.getInitialVoltage());
                    }
                });
            }

            @Override
            public void onFailure(String message) {

            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        //   Log.v("====","onActivityResult");
        if (requestCode == 1000 && resultCode == 1001) {

            String type = data.getStringExtra("backType");
            if (type != null) {
                equipmentType.setText(type);
            }
            typeText = equipmentType.getText().toString();

        } else if (requestCode == 1000 && resultCode == 1002) {
            String type = data.getStringExtra("backType");
            if (type != null) {
                enabling.setText(type);
            }
            enableText = equipmentType.getText().toString();
        }
    }
}